package com.epfresh.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.R;

/**
 * Created by wzh on 2016/5/9.
 */
public class CommonInputItem extends RelativeLayout {
    private TextView tvTitle;
    private EditText edInput;
    private TextView tvInput;
    private ImageView ivFlag;
    private View vBottomLine;

    private int titleEms;
    private int contentColor;
    private int inputType;
    private String content;
    private String contentHint;
    private String title;
    private Drawable flagSrc;
    private boolean isEdit;
    private boolean isCleanable;
    private boolean isSingleLine;
    private boolean isEllipsize;
    private boolean isFlag;
    private int textGravity;
    private int maxLength;
    private int textPaddingTop;
    private int textPaddingRight;
    private int textPaddingLeft;
    private int textPaddingBottom;

    public CommonInputItem(Context context) {
        this(context, null, 0);
    }

    public CommonInputItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public CommonInputItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CommonInputItem);

        maxLength = ta.getInt(R.styleable.CommonInputItem_content_text_length, -1);

        title = ta.getString(R.styleable.CommonInputItem_title_text);
        content = ta.getString(R.styleable.CommonInputItem_content_text);
        contentHint = ta.getString(R.styleable.CommonInputItem_content_text_hint);
        title = ta.getString(R.styleable.CommonInputItem_title_text);
        titleEms = ta.getInt(R.styleable.CommonInputItem_title_ems, -1);
        flagSrc = ta.getDrawable(R.styleable.CommonInputItem_flag_src);
        isEdit = ta.getBoolean(R.styleable.CommonInputItem_type_edit, false);
        isFlag = ta.getBoolean(R.styleable.CommonInputItem_flag, true);
        isCleanable = ta.getBoolean(R.styleable.CommonInputItem_cleanable, false);
        isEllipsize = ta.getBoolean(R.styleable.CommonInputItem_is_ellipsize, false);
        isSingleLine = ta.getBoolean(R.styleable.CommonInputItem_singleable, false);
        inputType = ta.getInt(R.styleable.CommonInputItem_inputType, -1);
        textGravity = ta.getInt(R.styleable.CommonInputItem_text_gravity, -1);
        textPaddingTop = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_text_padding_top,-1);
        textPaddingLeft = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_text_padding_left,-1);
        textPaddingRight = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_text_padding_right,-1);
        textPaddingBottom = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_text_padding_bottom,-1);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            contentColor = ta.getColor(R.styleable.CommonInputItem_content_text_color, getResources().getColor(R.color.font_color_def, null));

        } else {
            contentColor = ta.getColor(R.styleable.CommonInputItem_content_text_color, getResources().getColor(R.color.font_color_def));
        }
//        int titleColor = ta.getColor(R.styleable.CommonInputItem_title_color, getResources().getColor(R.color.item_title_color_def));
//        int titleWidth = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_title_width, LayoutParams.WRAP_CONTENT);
//        int titleHeight = ta.getDimensionPixelOffset(R.styleable.CommonInputItem_title_height, LayoutParams.WRAP_CONTENT);

        ta.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        View child = null;
        if (isEdit) {
            child = View.inflate(getContext(), R.layout.common_input_item, null);
            edInput = (EditText) child.findViewById(R.id.ed_input);
            edInput.setTextColor(contentColor);
            edInput.setText(content);
            edInput.setHint(contentHint);
            int paddingBottom = edInput.getPaddingBottom();
            int paddingTop = edInput.getPaddingTop();
            int paddingRight = edInput.getPaddingRight();
            int paddingLeft = edInput.getPaddingLeft();
            if (textPaddingLeft > 0){
                paddingLeft = textPaddingLeft;
            }
            if (textPaddingTop > 0){
                paddingTop = textPaddingTop;
            }
            if (textPaddingBottom > 0){
                paddingBottom = textPaddingBottom;
            }
            if (textPaddingRight > 0){
                paddingRight = textPaddingRight;
            }
            edInput.setPadding(paddingLeft,paddingTop,paddingRight,paddingBottom);
            if(maxLength>-1){
                edInput.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
            }
            if (textGravity >-1){
                edInput.setGravity(textGravity);
            }
        } else {
            child = View.inflate(getContext(), R.layout.common_item, null);
            tvInput = (TextView) child.findViewById(R.id.ed_input);
            tvInput.setTextColor(contentColor);
            tvInput.setText(content);
            tvInput.setHint(contentHint);
            if (isSingleLine){
                tvInput.setSingleLine();
            }
            if (isEllipsize){
                tvInput.setSingleLine();
                tvInput.setEllipsize(TextUtils.TruncateAt.END);
            }
            if(maxLength>-1){
                tvInput.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLength) });
            }
            if (textGravity >-1){
                tvInput.setGravity(textGravity);
            }
            int paddingBottom = tvInput.getPaddingBottom();
            int paddingTop = tvInput.getPaddingTop();
            int paddingRight = tvInput.getPaddingRight();
            int paddingLeft = tvInput.getPaddingLeft();
            if (textPaddingLeft > 0){
                paddingLeft = textPaddingLeft;
            }
            if (textPaddingTop > 0){
                paddingTop = textPaddingTop;
            }
            if (textPaddingBottom > 0){
                paddingBottom = textPaddingBottom;
            }
            if (textPaddingRight > 0){
                paddingRight = textPaddingRight;
            }
            tvInput.setPadding(paddingLeft,paddingTop,paddingRight,paddingBottom);

        }
        ivFlag = (ImageView) child.findViewById(R.id.iv_flag);
        tvTitle = (TextView) child.findViewById(R.id.tv_title);
        vBottomLine = child.findViewById(R.id.v_bottom_line);

        if (isEdit && isCleanable) {
            updateCleanable();
            edInput.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    updateCleanable();
                }
            });

            ivFlag.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    edInput.setText("");
                }
            });

        }

        if (!isFlag){
            ivFlag.setVisibility(View.GONE);
        }
        if (titleEms > 0) {
            tvTitle.setEms(titleEms);
        }
        if (flagSrc != null) {
            ivFlag.setImageDrawable(flagSrc);
        } else {
            ivFlag.setVisibility(View.VISIBLE);
            ivFlag.setClickable(false);
        }

        if (edInput != null && inputType > -1) {
            edInput.setInputType(inputType);
        }
        tvTitle.setText(title);
        addView(child);

    }
    public String getTitleText() {
        return tvTitle.getText().toString();
    }
    public String getContentText() {
        if (edInput != null) {
            return edInput.getText().toString();
        } else {
            return tvInput.getText().toString();
        }
    }

    public void setSelectionLast(){
        if (edInput != null){
            edInput.setSelection(edInput.length());
        }
    }

    public void setContentText(String text) {
        if (edInput != null) {
            edInput.setText(text);
        } else {
            tvInput.setText(text);
        }
    }
    public void setContentHintText(String text) {
        if (edInput != null) {
            edInput.setHint(text);
        } else {
            tvInput.setHint(text);
        }
    }

    public void setTitleText(String text) {
        tvTitle.setText(text);
    }

    public void setSelect(String title , boolean isSelect){

        String color = "#959595";

        if (isSelect){

            color = "#00b7ee";
        }
        if (edInput != null) {
            edInput.setHint(title);
            edInput.setHintTextColor(Color.parseColor(color));
        } else {
            tvInput.setHint(title);
            tvInput.setHintTextColor(Color.parseColor(color));
        }
    }

    public TextView getContent() {
        if (edInput != null) {
            return edInput;
        } else {
            return tvInput;
        }
    }

    public void setText(String text,int type){
        String color = "#959595";

        if (type > 0 ){
            color = "#00b7ee";
        }else if (type < 0 ){//未通过
            color = "#ff0000";
        }else {

        }
        if (edInput != null) {
            edInput.setHint(text);
            edInput.setHintTextColor(Color.parseColor(color));
        } else {
            tvInput.setHint(text);
            tvInput.setHintTextColor(Color.parseColor(color));
        }
    }

    boolean isNotClean;

    public void updateCleanable() {
        if (edInput != null && edInput.length() > 0 && !isNotClean) {
            ivFlag.setVisibility(View.VISIBLE);
            ivFlag.setClickable(true);
        } else if (edInput != null) {
            ivFlag.setVisibility(View.INVISIBLE);
            ivFlag.setClickable(false);
        }
    }

    public void setTextEnabled(boolean isEnable) {
        if (edInput != null) {
            edInput.setFocusable(isEnable);
            edInput.setFocusableInTouchMode(isEnable);
            if (isEnable){
//                edInput.requestFocus();
            }


        }
        if (tvInput != null) {
            tvInput.setEnabled(isEnable);
            this.setEnabled(isEnable);
        }
        isNotClean = !isEnable;
        if (!isEnable)
            ivFlag.setVisibility(View.GONE);
    }

    public void setHintEnable(boolean isEnable){
        if (edInput != null) {
            edInput.setFocusable(isEnable);
            edInput.setFocusableInTouchMode(isEnable);
            if (isEnable){
                edInput.setTextColor(Color.parseColor("#434343"));
            }else {
                edInput.setTextColor(Color.parseColor("#b3b3b3"));
            }
        }
        if (tvInput != null) {
            tvInput.setEnabled(isEnable);
            this.setEnabled(isEnable);
        }

    }

    public void setCanEdit(boolean isCanEdit){
        if (!isCanEdit){
            getChildAt(0).setBackgroundColor(Color.parseColor("#f5f5f5"));
            if(tvInput != null) {
                tvInput.setEnabled(isCanEdit);
                tvInput.setTextColor(Color.parseColor("#959595"));
            }
            if (edInput != null){
                edInput.setTextColor(Color.parseColor("#959595"));
                edInput.setEnabled(isCanEdit);
            }
            setEnabled(isCanEdit);
           ivFlag.setVisibility(INVISIBLE);
        }else {
            getChildAt(0).setBackgroundColor(Color.parseColor("#ffffff"));
            if(tvInput != null) {
                tvInput.setEnabled(isCanEdit);
                tvInput.setTextColor(Color.parseColor("#434343"));
            }
            if (edInput != null){
                edInput.setTextColor(Color.parseColor("#434343"));
                edInput.setEnabled(isCanEdit);
            }
            setEnabled(isCanEdit);
            ivFlag.setVisibility(INVISIBLE);
        }
    }

    public void showFlag(boolean show){
        if (show){
            ivFlag.setVisibility(View.VISIBLE);
        }else {
            ivFlag.setVisibility(View.INVISIBLE);
        }

    }

}
